package io.renren.controller;

import io.renren.common.utils.R;
import io.renren.entity.UserEntity;
import io.renren.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;

/**
 * mongoDB接口
 * @author luhongda
 * @email 634619275@qq.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/mongodb")
@Api(tags="mongoDB接口")
public class ApiMongoDBTest {

    @Autowired
    private UserService userService;

    @PostMapping("insert")
    @ApiOperation("插入")
    public R register(){
        UserEntity user = new UserEntity();
        user.setUserId(1L);
        user.setUsername("小明");
        user.setPassword("123456");
        user.setCreateTime(new Date());
        userService.saveUser(user);
        return R.ok();
    }

    @PostMapping("update")
    @ApiOperation("更新")
    public R update(){
        UserEntity user = new UserEntity();
        user.setUserId(1L);
        user.setUsername("小红");
        user.setPassword("123456");
        user.setCreateTime(new Date());
        userService.updateUser(user);
        return R.ok();
    }

    @PostMapping("deleteUserById")
    @ApiOperation("删除")
    public R deleteUserById(){
        userService.deleteUserById(1L);
        return R.ok();
    }

    @PostMapping("findUserByUserName")
    @ApiOperation("查询")
    public R findUserByUserName(){
        return R.ok().put("data",userService.findUserByUserName("小明"));
    }

}
